package com.hangzhou.xyj.ui.adapter;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DramaModel;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.AddFollowHandler;
import com.hangzhou.xyj.net.handler.CancelCollectHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.AddFollowReq;
import com.hangzhou.xyj.net.request.CancelCollectReq;
import com.hangzhou.xyj.net.request.CancelFollowReq;
import com.hangzhou.xyj.ui.activity.DynamicDetailActivity;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicImgViewHolder;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicListenViewHolder;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicTxtViewHolder;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * 我的收藏Adapter
 *
 * @author Administrator
 */
public class MyCollectionAdapter extends BaseLoadingAdapter<DynamicModel> {

    public MyCollectionAdapter(RecyclerView recyclerView, List<DynamicModel> list) {
        super(recyclerView, list);
    }

    @Override
    public void onBindNormalViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        switch (getItemViewType(position)) {
            case DynamicModel.TYPE_TEXT:
                ((DynamicTxtViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_MY_COLLECTION.getValue());
                ((DynamicTxtViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_MY_COLLECTION.getValue());
                ((DynamicTxtViewHolder) viewHolder).setData(mDataList.get(position));
                ((DynamicTxtViewHolder) viewHolder).mLlMoreMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new MyOnClickListener(position, viewHolder.getAdapterPosition()).onClick(v);
                    }
                });
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DynamicModel model = mDataList.get(viewHolder.getAdapterPosition());
                        Intent intent = new Intent(mContext, DynamicDetailActivity.class);
                        intent.putExtra(DataKey.INDEX, model.getId());
                        intent.putExtra(DataKey.TITLE, model.getUserName());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case DynamicModel.TYPE_LISTEN:
                final DynamicModel dynamicModel = mDataList.get(position);
                ((DynamicListenViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_MY_COLLECTION.getValue());
                ((DynamicListenViewHolder) viewHolder).setData(dynamicModel);
//                ((DynamicListenViewHolder) viewHolder).mLlMoreMenu.setOnClickListener(new MyOnClickListener(position, viewHolder.getAdapterPosition()));
                ((DynamicListenViewHolder) viewHolder).mLlMoreMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new MyOnClickListener(position, viewHolder.getAdapterPosition()).onClick(v);
                    }
                });
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DynamicModel dynamicModel = mDataList.get(viewHolder.getAdapterPosition());
                        DramaModel dramaModel = new DramaModel();
                        dramaModel.setId(dynamicModel.getId());
                        dramaModel.setName(dynamicModel.getSongName());
                        dramaModel.setNativeUrl(dynamicModel.getNativeUrl());
                        dramaModel.setAccompanyUrl(dynamicModel.getAccompanyUrl());
                        dramaModel.setLrcUrl(dynamicModel.getLrcUrl());
                        IntentManager.getInstance().goDramaPlayActivity(mContext, dramaModel);
                    }
                });
                break;
            case DynamicModel.TYPE_IMG:
                ((DynamicImgViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_MY_COLLECTION.getValue());
                ((DynamicImgViewHolder) viewHolder).setData(mDataList.get(position));
                ((DynamicImgViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_MY_COLLECTION.getValue());
                ((DynamicImgViewHolder) viewHolder).mLlMoreMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new MyOnClickListener(position, viewHolder.getAdapterPosition()).onClick(v);
                    }
                });
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DynamicModel model = mDataList.get(viewHolder.getAdapterPosition());
                        Intent intent = new Intent(mContext, DynamicDetailActivity.class);
                        intent.putExtra(DataKey.INDEX, model.getId());
                        intent.putExtra(DataKey.TITLE, model.getUserName());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        switch (viewType) {
            case DynamicModel.TYPE_IMG:
                view = mLayoutInflater.inflate(R.layout.layout_active_item_img, viewGroup, false);
                return new DynamicImgViewHolder(mContext, view);
            case DynamicModel.TYPE_LISTEN:
                view = mLayoutInflater.inflate(R.layout.layout_active_item_listen, viewGroup, false);
                return new DynamicListenViewHolder(mContext, view);
            case DynamicModel.TYPE_TEXT:
            default:
                view = mLayoutInflater.inflate(R.layout.layout_active_item_text, viewGroup, false);
                return new DynamicTxtViewHolder(mContext, view);
        }
    }


    @Override
    public int getItemViewType(int position) {
        int viewType = super.getItemViewType(position);
        if (viewType == TYPE_LOADING_ITEM) {//加载条目
            return viewType;
        } else {
            return mDataList.get(position).getRealShowType();
        }
    }

    /**
     * 添加关注
     *
     * @param followId 要关注的人的ID
     * @param position 数组中的位置
     * @return
     * @author yaohuix
     * @date 2016/3/22 19:51
     */
    public void addFollow(int followId, final int position) {
        AddFollowReq addFollowReq = new AddFollowReq(AddFollowReq.TYPE_USER, followId,
                Integer.valueOf(XYJApplication.getUserInfo().getUserId()));
        HttpLoader.getInstance().sendRequest(new AddFollowHandler(addFollowReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("关注成功！");
                    mDataList.get(position).setIsFollow(true);
                }
            }
        }, 0));
    }

    /**
     * 取消关注
     *
     * @param followId 要关注的人的ID
     * @param position 数组中的位置
     * @return
     * @author yaohuix
     * @date 2016/4/8 10:34
     */
    public void cancelFollow(int followId, final int position) {
        CancelFollowReq cancelFollowReq = new CancelFollowReq(CancelFollowReq.TYPE_USER, followId,
                Integer.valueOf(XYJApplication.getUserInfo().getUserId()));
        HttpLoader.getInstance().sendRequest(new AddFollowHandler(cancelFollowReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("取消关注成功！");
                    mDataList.get(position).setIsFollow(false);
                }
            }
        }, 0));
    }

    /**
     * 取消收藏
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/8 11:05
     */
    public void cancelCollect(int collectId, final int adapterPosition) {
        CancelCollectReq cancelCollectReq = new CancelCollectReq(collectId);
        HttpLoader.getInstance().sendRequest(new CancelCollectHandler(cancelCollectReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mDataList.remove(adapterPosition);
                notifyItemRemoved(adapterPosition);
            }
        }, 0));
    }

    /**
     * 点击下拉箭头，实现的接口
     */
    class MyOnClickListener implements View.OnClickListener {
        private int position;
        private int adapterPosition;

        public MyOnClickListener(int position, int adapterPositon) {
            this.position = position;
            this.adapterPosition = adapterPositon;
        }

        @Override
        public void onClick(View v) {
            SelectMenuDialog menuDialog = new SelectMenuDialog(mContext);
            String[] menu;
            Log.d("debug", "当前的size  = " + mDataList.size() + "adapterPosition = " + adapterPosition);

            if (mDataList.get(adapterPosition).getIsFollow()) {
                menu = new String[]{"取消关注", "取消收藏"};
            } else {
                menu = new String[]{"关注", "取消收藏"};
            }
            menuDialog.setMenu(menu, new SelectMenuDialog.SelectMenuClickListener() {
                @Override
                public void onSelectMenuClick(Dialog dialog, View item, int inneerPosition) {
                    dialog.dismiss();
                    switch (inneerPosition) {
                        case 0://取消关注 或者 关注
                            if (mDataList.get(adapterPosition).getIsFollow()) {
                                //取消关注（我的收藏的userId特殊处理）
                                cancelFollow(mDataList.get(adapterPosition).getUser_id(), adapterPosition);
                            } else {
                                //关注（我的收藏的userId特殊处理）
                                addFollow(mDataList.get(adapterPosition).getUser_id(), adapterPosition);
                            }
                            break;
                        case 1://取消收藏
                            cancelCollect(mDataList.get(adapterPosition).getCollectId(), adapterPosition);
                            break;
                        default:
                            break;
                    }
                }
            });
            menuDialog.show();
        }
    }

}
